package org.ch00.test13;

/*
 *
 *@author XCH
 *@date 2025 - 03 - 12 - 16:26
 *
 */
public class UserThread extends Thread{
    private Printer printer;
    private String user;
    private String content;
    private int mode; // 模式：1=对象锁, 2=同步块, 3=类锁, 4=静态同步块

    public UserThread(Printer printer, String user, String content, int mode) {
        this.printer = printer;
        this.user = user;
        this.content = content;
        this.mode = mode;
    }

    @Override
    public void run() {
        System.out.println(user + "开始打印");
        switch (mode) {
            case 1:
                System.out.println("使用对象锁");
                printer.print(user, content);
                break;
            case 2:
                System.out.println("使用同步块");
                printer.printWithBlock(user, content);
                break;
            case 3:
                System.out.println("使用类锁");
                Printer.printWithClassLock(user, content);
                break;
            case 4:
                System.out.println("使用静态同步块");
                printer.printWithStaticBlock(user, content);
                break;
        }
    }
}
